import cloudbase from '@cloudbase/js-sdk'
import { Loading } from 'element-ui'
export function cloudbaseApp() {
  return cloudbase.init({
    env: 'sgb-8gqmg7kv48722f4e'
  })
}
export function development() {
  const env = 'prod'
  // 依次是 诗歌本、花朵、版本控制、话语的集合名称、云存储路径、话语类型
  let SGBDBNAME, HDBDBNAME, VSDBNAME, HYDBNAME, UPLOADPATH
  const SHIJIDB = 'shijidb'; const HYTYPE = 'huayu-type-new'; const HDINDEXIMG = 'flower-garden-index-img'; const JINWENGDB = 'jingWenDb'; const HDIMGDBNAME = 'flower-garden-index-img'; const JINGWENDB = 'jingWenDb'
  const USERDB = 'user-action-log-dev'
  if (env === 'prod') {
    SGBDBNAME = 'zanmeidb-new'
    HDBDBNAME = 'flower-garden-list'
    VSDBNAME = 'sgb-version-control'
    HYDBNAME = 'huayu-db-new'
    UPLOADPATH = 'sgb/nad/'
  } else if (env === 'dev') {
    SGBDBNAME = 'zanmeidb-dev'
    HDBDBNAME = 'flower-garden-list-dev'
    VSDBNAME = 'sgb-version-control-dev'
    HYDBNAME = 'huayu-db-new-dev'
    UPLOADPATH = 'sgb/dev/'
  }
  return { HDBDBNAME, SGBDBNAME, VSDBNAME, HYDBNAME, SHIJIDB, UPLOADPATH, HYTYPE, HDINDEXIMG, JINWENGDB, HDIMGDBNAME, JINGWENDB, USERDB }
}
/**
 * 上传文件
 * @param cloudPath {云端路径+文件名}
 * @param filePath {本地文件对象}
 * @returns {Promise<cloudbase.storage.ICloudbaseUploadFileResult>}
 */
export function cloudbaseUpload(cloudPath, filePath) {
  return cloudbaseApp().uploadFile({ cloudPath, filePath })
}
export function cloud_md5(str) {
  const md5 = require('md5')
  return md5(str)
}
export function loadingFull() {
  return Loading.service({ fullscreen: true })
}
export function dateFormat(date, format) {
  var o = {
    'M+': date.getMonth() + 1, // month
    'd+': date.getDate(), // day
    'h+': date.getHours(), // hour
    'm+': date.getMinutes(), // minute
    's+': date.getSeconds(), // second
    'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
    'S': date.getMilliseconds() // millisecond
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1,
      (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1,
        RegExp.$1.length === 1 ? o[k]
          : ('00' + o[k]).substr(('' + o[k]).length))
    }
  }
  return format
}
